<template>
  <el-dialog v-model="open" :title="title" width="1000px" append-to-body :close-on-click-modal="false">
    <el-form :model="baseFormData" label-width="150px">
      <el-form-item label="logo" required>
        <div class="avatarUrl">
          <ImageUpload v-model="baseFormData.imgUrl" :limit="1"/>
        </div>
      </el-form-item>
      <el-form-item label="参赛队名称" required>
        <el-input v-model="baseFormData.name"  placeholder="请输入参赛队名称"/>
      </el-form-item>
      <el-form-item label="参赛队简称" required>
        <el-input v-model="baseFormData.abreviations"  placeholder="请输入参赛队简称"/>
      </el-form-item>

      <el-form-item label="参赛队类型" required>
        <el-select v-model="baseFormData.type" class="m-2" placeholder="Select" size="large">
          <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.text"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" required>
        <el-input v-model="baseFormData.contactPerson"  placeholder="请输入联系人姓名"/>
      </el-form-item>
      <el-form-item label="联系方式" required>
        <el-input v-model="baseFormData.contactTelno"  placeholder="请输入联系方式"/>
      </el-form-item>

      <el-form-item label="所在地区" required>
        <el-cascader
            v-model="baseFormData.regionId"
            :options="regionsList"
            @change="handleChange"
            :props="{ label:'text' }"
        />
      </el-form-item>
      <el-form-item label="详细地址" required>
        <el-input v-model="baseFormData.address"  placeholder="请输入详细地址"/>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer text-center">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { getCurrentInstance, reactive } from '@vue/runtime-core'
import { watch } from 'vue'
import { toRefs } from '@vueuse/shared'
import { nationList, certificates } from '@/assets/lib/nation'
import { countryList } from '@/assets/lib/country'
const { proxy } = getCurrentInstance()

const props = defineProps({
  props: {
    type: Object,
    default: () => ({
      open: false,
      title: '参赛队详情'
    })
  }
})
const emit = defineEmits(['submitForm'])
const formBase = {
  validityType: '0',
  status: '1',
  useWorkdayFlag: '0',
  useHolidayFlag: '0'
}
const data = reactive({
  form: {
	    ...formBase
  },
  rules: {
    name: [{ required: true, message: '必填', trigger: 'blur' }],
    nation: [{ required: true, message: '必填', trigger: 'change' }],
    country: [{ required: true, message: '必填', trigger: 'change' }],
    idType: [{ required: true, message: '必填', trigger: 'change' }],
    idNum: [{ required: true, message: '必填', trigger: 'blur' }],
    phone: [{ required: true, message: '必填', trigger: 'blur' }],
    area: [{ required: true, message: '必填', trigger: 'change' }],
    address: [{ required: true, message: '必填', trigger: 'blur' }],
    photo: [{ required: true, message: '必填', trigger: 'blur' }],
    sex: [{ required: true, message: '必填', trigger: 'change' }],
    tag: [{ required: true, message: '必填', trigger: 'change' }]
  }
})
const { open, title } = toRefs(props.props)
const { form, rules } = toRefs(data)
watch(open, (val) => {
  if (val) {
    form.value = props.props.data || { ...formBase }
  } else {
    proxy.resetForm('dialogRef')
  }
})
function submitForm() {
  proxy.$refs['dialogRef'].validate((valid) => {
    if (valid) {
      open.value = false
      emit('submitForm')
    }
  })
}
function cancel() {
  open.value = false
}
</script>

<style lang="scss">

	.tip{font-size: 13px;color: #999;margin: 10px 0;
		i{color: red;margin: 0 4px 0 0;}
	}
</style>
